/**
 * 
 */
package kr.pe.johnkim.dayve.provider;

import java.util.ArrayList;

import kr.pe.johnkim.dayve.activity.R;

import android.R.color;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.AnimationDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * Dayve 카테고리 어댑터 클래스 이다.
 * 
 * @author johnkim
 *
 */
public class DayveCategoryAdapter extends BaseAdapter {
	
	public static final String TAG = DayveCategoryAdapter.class.getSimpleName();
	
	private Context context;
	private ArrayList<DayveCategory> data;
	private DayveCategory item;
	LayoutInflater inf;
	
	View view;

	public DayveCategoryAdapter(Context context, ArrayList<DayveCategory> data) {
		this.context = context;
		this.data = data;
		inf = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public int getCount() {
		// TODO Auto-generated method stub
		return data.size();
	}

	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return data.get(position);
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		view = convertView;
		
		ViewWrapper wrapper = null;
		
		if(view == null) {
			view = inf.inflate(R.layout.dayve_category_row, parent, false);
			wrapper = new ViewWrapper(view);
			view.setTag(wrapper);
		} else {
			wrapper = (ViewWrapper)view.getTag();
		}

		item = data.get(position);
		
//		Log.i(TAG, "favoriteYn  : " + item.getFavoriteYn());
//		Log.i(TAG, "name        : " + item.getName());
//		Log.i(TAG, "items count : " + item.getItemsCount());
		
		// 즐겨찾기 유무를 세팅 한다.
//		if(("Y").equals(item.getFavoriteYn())) {
//			wrapper.getLinearLayoutCategoryFavoriteYn().setBackgroundColor(Color.YELLOW);
//		} else {
//			wrapper.getLinearLayoutCategoryFavoriteYn().setBackgroundColor(Color.rgb(188, 188, 188));
//		}
//		
//		// 배경 컬러를 세팅 한다.
//		wrapper.getLinearLayoutCategoryFavoriteName().setBackgroundColor(Color.rgb(188, 188, 188));
//		wrapper.getLinearLayoutCategoryFavoriteItem1().setBackgroundColor(Color.rgb(242, 242, 242));
//		wrapper.getLinearLayoutCategoryFavoriteItem2().setBackgroundColor(Color.rgb(242, 242, 242));
//		
//		// 카테고리명을 세팅 한다 : 카테고리명 앞뒤로 약간의 공백을 둔다.
//		wrapper.getTxtViewCategoryName().setText("  " + item.getName() + "  ");
//		// 카테고리에 등록된 아이템의 총 갯수를 세팅 한다.
//		wrapper.getTxtViewCategoryItemCount().setText(item.getItemsCount());
		// 카테고리에 가장 최근에 등록된 아이템의 등록 일자를 세팅 한다.
//		wrapper.getTxtViewCategoryItemRegisterDate().setText(item.getCategoryItemRegisterDate());
		
		if(("Y").equals(item.getFavoriteYn())) {
			wrapper.getImgViewCategoryGridFavorite().setVisibility(View.VISIBLE);
		} else {
			wrapper.getImgViewCategoryGridFavorite().setVisibility(View.INVISIBLE);
		}
		
		int lid = context.getResources().getIdentifier(item.getIconUrl(), "drawable", context.getPackageName());
		
		wrapper.getImgViewCategoryGridIcon().setImageResource(lid);
		wrapper.getTxtViewCategoryGridCategoryName().setText(item.getName());
		
		return view;
	}
	
	/**
	 * View Wrapper class.
	 * 
	 * @author johnkim
	 *
	 */
	class ViewWrapper {
		View base;
		
		ImageView imgViewCategoryGridFavorite;
		ImageView imgViewCategoryGridIcon;
		TextView txtViewCategoryGridCategoryName;
		
		ViewWrapper(View base) {
			this.base = base;
		}
		
		ImageView getImgViewCategoryGridFavorite() {
			if(imgViewCategoryGridFavorite == null) {
				imgViewCategoryGridFavorite = (ImageView)view.findViewById(R.id.imgViewCategoryGridFavorite);
			}
			
			return (ImageView)view.findViewById(R.id.imgViewCategoryGridFavorite);
		}
		
		ImageView getImgViewCategoryGridIcon() {
			if(imgViewCategoryGridIcon == null) {
				imgViewCategoryGridIcon = (ImageView)view.findViewById(R.id.imgViewCategoryGridIcon);
			}
			
			return (imgViewCategoryGridIcon);
		}
		
		TextView getTxtViewCategoryGridCategoryName() {
			if(txtViewCategoryGridCategoryName == null) {
				txtViewCategoryGridCategoryName = (TextView)view.findViewById(R.id.txtViewCategoryGridCategoryName);
			}
			
			return (txtViewCategoryGridCategoryName);
		}
	}

}
